Completed
Push — master ( 4bb4df...375c4e )
by Sander
10s
created

angular.directive(ꞌcredentialFieldꞌ)   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
nc 7
nop 0
dl 0
loc 13
rs 8.8571
c 1
b 0
f 0
1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
	/**
26
	 * @ngdoc directive
27
	 * @name passmanApp.directive:passwordGen
28
	 * @description
29
	 * # passwordGen
30
	 */
31
32
	angular.module('passmanApp')
33
		.directive('credentialField', ['$timeout', '$translate', function ($timeout, $translate) {
34
			return {
35
				scope: {
36
					value: '=value',
37
					secret: '=secret',
38
					inputField: '=useInput',
39
					inputFieldplaceholder: '=inputPlaceholder'
40
				},
41
				restrict: 'A',
42
				replace: 'true',
43
				template: "" +
44
				'<span class="credential_field">' +
45
				'<div class="value" ng-class="{\'ellipsis\': isLink}">' +
46
				'<span ng-if="secret"><span ng-repeat="n in [] | range:value.length" ng-if="!valueVisible">*</span></span>' +
47
				'<span ng-if="valueVisible && !inputField" ng-bind-html="value"></span>' +
48
				'<span ng-if="valueVisible && inputField"><input type="text" ng-model="value" select-on-click placeholder="{{ inputFieldplaceholder }}!"</span>' +
49
				'</div>' +
50
				'<div class="tools">' +
51
				'<div class="cell" ng-if="toggle" tooltip="tggltxt" ng-click="toggleVisibility()"><i class="fa" ng-class="{\'fa-eye\': !valueVisible, \'fa-eye-slash\': valueVisible }"></i></div>' +
52
				'<div class="cell" ng-if="isLink"><a ng-href="{{value}}" target="_blank" rel="nofollow noopener noreferrer"><i tooltip="\'Open in new window\'" class="link fa fa-external-link"></i></a></div>' +
53
				'<div class="cell" ngclipboard-success="onSuccess(e);" ngclipboard-error="onError(e);" ngclipboard data-clipboard-text="{{value}}"><i  tooltip="copy_msg" class="fa fa-clipboard"></i></div>' +
54
				'</div></span>',
55
				link: function (scope) {
56
					var expression = /(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/gi;
57
					var regex = new RegExp(expression);
58
					$translate(['toggle.visibility', 'copy', 'copied']).then(function (translations) {
59
						scope.tggltxt = translations['toggle.visibility'];
60
						scope.copy_msg = translations['copy.field'];
61
					});
62
63
					scope.$watch("value", function () {
64
						if (scope.value) {
65
							if (scope.secret) {
66
								scope.valueVisible = false;
67
							}
68
							if (regex.test(scope.value)) {
69
								scope.isLink = true;
70
								if(scope.value.substr(0,4) !== 'http'){
71
									scope.value = 'http://'+scope.value;
72
								}
73
							}
74
						}
75
					});
76
					if (!scope.toggle) {
77
						if (scope.secret) {
78
							scope.toggle = true;
79
						}
80
					}
81
82
					var timer;
83
					scope.onSuccess = function () {
84
						scope.copy_msg = $translate.instant('copied') ;
85
						$timeout.cancel(timer);
86
						timer = $timeout(function () {
87
							scope.copy_msg = $translate.instant('copy');
88
						}, 5000);
89
					};
90
					scope.valueVisible = true;
91
					scope.toggleVisibility = function () {
92
						scope.valueVisible = !scope.valueVisible;
93
					};
94
				}
95
			};
96
		}]);
97
98
}());
99